﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.VisualStudio.TeamArchitect.PowerTools.Ultimate.Features;
using System.ComponentModel.Composition;

namespace YasharEl.Infrastructure.ArchitectureTools.AppModeler
{
    /// <summary>
    /// Overrides of 3 methods which allow this launch point
    /// execute even when the feature is not yet instantiated
    /// </summary>
    public partial class LaunchAppModelerGuidance : VsLaunchPoint
    {
        [Import]
        IFeatureManager FeatureManger { get; set; }

        /// <summary>
        /// The VsLaunchPoint connects, by default, to a IFeatureCommand via
        /// a Command Binding.  However, CommandBindings are always associated
        /// with a Feature Extension instance and, since we don't have one
        /// yet, we must put our code right here in the Execute method
        /// </summary>
        /// <param name="feature"></param>
        public override void Execute(IFeatureExtension feature)
        {
            IFeatureExtension instantiatedFeature = null;
            try
            {
                // First, see if our instance is already activated and,
                // if so, just make it the active FX
                foreach (IFeatureExtension fx in FeatureManager.InstantiatedFeatures)
                {
                    if (fx.InstanceName == Constants.FeatureName)
                    {
                        instantiatedFeature = fx;
                        return;
                    }
                }

                instantiatedFeature = FeatureManager.Instantiate(Constants.FeatureName, Constants.FeatureName);
            }
            catch(Exception e)
            {
                System.Diagnostics.Debug.WriteLine(e.ToString());
            }
            finally
            {
                if (instantiatedFeature != null)
                    FeatureManager.ActiveFeature = instantiatedFeature;
            }
        }

        /// <summary>
        /// For standalone FX, always return null
        /// </summary>
        /// <param name="command">ignored</param>
        /// <returns>Always returns null</returns>
        protected override IFeatureExtension OnQueryStatus(Microsoft.VisualStudio.Modeling.ExtensionEnablement.IMenuCommand command)
        {
            return null;
        }

        /// <summary>
        /// For standalone FX, always return false
        /// </summary>
        /// <param name="feature">N/A</param>
        /// <returns>Always returns false</returns>
        public override bool CanExecute(IFeatureExtension feature)
        {
            return false;
        }

        /// <summary>
        /// Since we have no FX instance (as yet), always return true
        /// in order to get the menu item to appear
        /// </summary>
        public override bool Visible
        {
            get
            {
                return true;
            }
        }
    }
}
